package com.example.tvrecyclerview.activity;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tvrecyclerview.R;
import com.example.tvrecyclerview.adapter.GameListAdapter;
import com.example.tvrecyclerview.adapter.MyRecycleViewAdapter;
import com.example.tvrecyclerview.bean.GameListBean;
import com.example.tvrecyclerview.view.FocusKeepRecyclerView;
import com.example.tvrecyclerview.view.FocusRecyclerView;
import com.example.tvrecyclerview.view.SpaceDecoration;
import com.example.tvrecyclerview.view.TvRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: njb
 * @date: 2020/7/12 0012 23:33
 * @desc: 竖直的Tab切换，仿TV频道切换
 */
public class VerticalTabActivity extends AppCompatActivity implements MyRecycleViewAdapter.OnItemClickListener{

    private MyRecycleViewAdapter adapter;
    private FocusKeepRecyclerView recyclerView, rvGameList,rvTimeList;
    //标题
    private String[] titles = {"首页", "游戏", "教育", "生活", "娱乐", "新闻", "直播", "我的"};
    private GameListAdapter gameListAdapter;
    private List<GameListBean> gameListBeans;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vertical_recyclerview);
        initView();
        initAdapter();
        initGameListAdapter();
    }
    private void initView() {
        recyclerView = findViewById(R.id.rv_tab);
        rvGameList = findViewById(R.id.rv_game_list);
    }


    private void initGameListAdapter() {
        setData();
        gameListAdapter = new GameListAdapter(gameListBeans, this, new GameListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

            }
        }) {
            @Override
            protected void onItemFocus(View itemView) {
                itemView.setSelected(true);
                View view = itemView.findViewById(R.id.iv_bg);
                view.setSelected(true);
            }

            @Override
            protected void onItemGetNormal(View itemView) {
                itemView.setSelected(true);
                View view = itemView.findViewById(R.id.iv_bg);
                view.setSelected(true);
            }
        };
        rvGameList.setLayoutManager(new GridLayoutManager(this,5));
        rvGameList.addItemDecoration(new SpaceDecoration(30));
        rvGameList.setAdapter(gameListAdapter);
    }

    private void initAdapter() {
        adapter = new MyRecycleViewAdapter(this, titles);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter);
        recyclerView.requestFocus();
        adapter.setOnItemClickListener(this);
    }

    private void updateData(String title) {
        if (gameListBeans != null && gameListBeans.size() > 0) {
            gameListBeans.clear();
        }
        for (int i = 0; i < 40; i++) {
            GameListBean gameListBean = new GameListBean();
            gameListBean.setGameName(title);
            gameListBean.setImg("2");
            gameListBeans.add(gameListBean);

        }
        gameListAdapter.notifyDataSetChanged();
    }

    private void setData() {
        gameListBeans = new ArrayList<>();
        for (int i = 0; i < 40; i++) {
            GameListBean gameListBean = new GameListBean();
            gameListBean.setGameName("王者荣耀");
            gameListBean.setImg("2");
            gameListBeans.add(gameListBean);
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        updateData(titles[position]);
    }
}
